﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OleDb;
using System.Data;
/// <summary>
///KeyWord 的摘要说明
/// </summary>
public class KeyWord
{
	public KeyWord()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    public OleDbDataAdapter GetKeyAll()
    {

        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbDataAdapter odp = new OleDbDataAdapter("select * from [KeyWord]  Order BY ID DESC", myconn);
        return odp;
    }

    public int GetKeyCount()
    {
        int s = 0;
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select Count(*) from [KeyWord] ", myconn);

        try
        {
            myconn.Open();
            s = int.Parse(mycmd.ExecuteScalar().ToString());
        }

        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }

        return s;
    }
    public OleDbDataReader GetKeyConn(int ID)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select * from [KeyWord] where ID=" + ID + " ", myconn);
        OleDbDataReader dr = null;
        try
        {
            myconn.Open();
            dr = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }

        return dr;
    }
    public int ADDKey(string keyword, string geturl)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("Insert into KeyWord(keyword,geturl) Values ('" + keyword + "','" + geturl + "')", myconn);
        int s = 0;
        try
        {
            myconn.Open();
            s = mycmd.ExecuteNonQuery();

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        myconn.Close();
        return s;
    }
    public int UPPKey(string keyword, string geturl, int ID)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("UPdate [KeyWord] Set keyword='" + keyword + "',geturl='" + geturl + "' where ID=" + ID + "", myconn);
        int s = 0;
        try
        {
            myconn.Open();
            s = mycmd.ExecuteNonQuery();

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        myconn.Close();
        return s;
    }
    public DataSet GetKeyPage(int SPage1, int SPage2, string Sname)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select * from [KeyWord] Order By ID DESC", myconn);
        OleDbDataAdapter dr = new OleDbDataAdapter(mycmd);
        DataSet da = new DataSet();
        try
        {
            myconn.Open();
            dr.Fill(da, SPage1, SPage2, Sname);
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        return da;
    }
    //删除数据
    public int DelKey(int ID)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("delete from [KeyWord] where ID = @ID ", myconn);
        mycmd.Parameters.AddWithValue("@ID", ID);
        int nResult = -1;
        try
        {
            myconn.Open();
            nResult = mycmd.ExecuteNonQuery();
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        return nResult;
    }
}